let baseUrl = 'http://127.0.0.1:8069'; //域名或选取所有接口不变的那一部分
const db_name = 'odoo15'

export const request = (options = {}) => {
	//异步封装接口，使用Promise处理异步请求
	return new Promise((resolve, reject) => {
		// 发送请求
		uni.request({
			url: baseUrl + options.url || '', //接收请求的API
			method: options.method || 'GET', //接收请求的方式,如果不传默认为GET
			data: options.data || {}, //接收请求的data,不传默认为空
		}).then(data => {
			let [err, res] = data;
			resolve(res);
		}).catch(error => {
			reject(error);
		})
	})
}

const odooLogin = function(username = 'admin', password = '123456') {
	return request({
		url: '/web/session/authenticate',
		method: 'post',
		data: {
			"jsonrpc": "2.0",
			"method": "call",
			"params": {
				'db': db_name,
				'login': username,
				'password': password
			},
			"id": Math.floor(Math.random() * 1000000000 + 1)
		}
	})
}

const odooLogout = function(username = 'admin', password = '123456') {
	return request({
		url: '/web/session/destroy',
		method: 'post',
		data: {
			"jsonrpc": "2.0",
			"method": "call",
			"params": {},
			"id": Math.floor(Math.random() * 1000000000 + 1)
		}
	})
}

const odooBaseApi = function(model_name = 'container.status') {
	return {
		// 查询列表
		list: function(option = {
			limit: 20,
			offset: 1,
			domain: {}
		}) {
			let filter = []
			for (let key in option.domain) {
				filter.push([key, '=', option.domain[key]])
			}
			return request({
				url: `/web/dataset/call_kw/${model_name}/search_read`,
				method: 'post',
				data: {
					"id": Math.floor(Math.random() * 1000000000 + 1),
					"jsonrpc": "2.0",
					"method": "call",
					"params": {
						"model": model_name,
						"method": "search_read", // 使用的方法
						"args": [],
						"kwargs": {
							"domain": filter.length > 0 ? filter : [],
							"fields": [],
							"limit": option.limit,
							"offset": option.offset,
							"order": "name",
							"context": {
								"lang": "zh_CN",
								"tz": "Asia/Shanghai",
								// "uid": 2,
								// "allowed_company_ids": [1]
							}
						}
					},
				}
			})
		},
		// 查询单一
		retrieve: (id) => request({
			url: `/web/dataset/call_kw/${model_name}/read`,
			method: 'post',
			data: {
				"jsonrpc": "2.0",
				"method": "call",
				"id": Math.floor(Math.random() * 1000000000 + 1),
				"params": {
					"args": [
						[id],
					],
					"model": model_name,
					"method": "read",
					"kwargs": {
						"context": {
							"bin_size": true,
							"lang": "zh_CN",
							"tz": false,
							// "uid": 2,
							// "allowed_company_ids": [1]
						}
					}
				},
			}
		}),
		// 更新
		update: (id, params) => request({ // params的类型是对象
			url: `/web/dataset/call_kw/${model_name}/write`,
			method: 'post',
			data: {
				"jsonrpc": "2.0",
				"method": "call",
				"id": Math.floor(Math.random() * 1000000000 + 1),
				"params": {
					"args": [
						[id], params
					],
					"model": model_name,
					"method": "write",
					"kwargs": {
						"context": {
							"lang": "zh_CN",
							"tz": false,
							// "uid": 2,
							// "allowed_company_ids": [1]
						}
					}
				},
			}

		}),
		// 新建
		create: (params) => request({ // params的类型是对象
			url: `/web/dataset/call_kw/${model_name}/create`,
			method: 'post',
			data: {
				"jsonrpc": "2.0",
				"method": "call",
				"id": Math.floor(Math.random() * 1000000000 + 1),
				"params": {
					"args": [params],
					"model": "manage.cargo",
					"method": "create",
					"kwargs": {
						"context": {
							"lang": "en_US",
							"tz": false,
							// "uid": 2,
							// "allowed_company_ids": [1]
						}
					}
				},
			}

		}),
		// 删除
		delete: () => request({}),
	}
}

// 托盘模型各个字段
const WAREHOUSE_TRAY_MODEL = 'warehouse.tray'
export const warehouseTrayModel = odooBaseApi(WAREHOUSE_TRAY_MODEL)

// 托盘组模型各个字段
const WAREHOUSE_TRAY_GROUP_MODEL = 'warehouse.tray.group'
export const warehouseTrayGroupModel = odooBaseApi(WAREHOUSE_TRAY_GROUP_MODEL)

// 库位模型各个字段
const WAREHOUSE_PARTITION_LOCATION_MODEL = 'warehouse.partition.location'
export const warehousePartitionLocationModel = odooBaseApi(WAREHOUSE_PARTITION_LOCATION_MODEL)

// 库区模型各个字段
const WAREHOUSE_PARTITION_MODEL = 'warehouse.partition'
export const warehousePartitionModel = odooBaseApi(WAREHOUSE_PARTITION_MODEL)

// 货物模型各个字段
const MANAGE_CARGO_MODEL = 'manage.cargo'
export const manageCargoModel = odooBaseApi(MANAGE_CARGO_MODEL)

// 货柜模型各个字段
const CONTAINER_STATUS_MODEL = 'container.status'
export const containerStatusModel = odooBaseApi(CONTAINER_STATUS_MODEL)

// 物流运货单各个字段
const SPEDITION_SHEET_MODEL = 'spedition.sheet'
export const speditionSheetModel = odooBaseApi(SPEDITION_SHEET_MODEL)

// 本地测试
const LOCAL_TEST = 'payment.regulation.merchant.list'
export const localTestModel = odooBaseApi(LOCAL_TEST)

export {
	odooBaseApi,
	odooLogin,
	odooLogout
}
